package com.slf.engine.services.impl;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.ISzgjService;
import com.slf.engine.utils.KeyedDigestMD5;

public class SzgjServiceImpl implements ISzgjService {

	private static final Logger log = Logger.getLogger(SzgjServiceImpl.class);
	
	public Order doPay(Order order)
	{
		HttpClientManager clientManager = new HttpClientManager();
		try {
			StringBuffer url = new StringBuffer(SysContants.SZGJ_URL);
			url.append("?haoma=" + order.getAccount());
			url.append("&jiaofeijine=" + order.getMoney());
			url.append("&clientorderid=" + order.getOrderId());
			url.append("&username=" + SysContants.SZGJ_LOGINNAME);
			url.append("&userpwd=" + KeyedDigestMD5.getKeyedDigest(KeyedDigestMD5.getKeyedDigest(SysContants.SZGJ_PASSWORD, ""), ""));
			url.append("&zhifumima=" + KeyedDigestMD5.getKeyedDigest(KeyedDigestMD5.getKeyedDigest(SysContants.SZGJ_PAYPWD, ""), ""));
			url.append("&noticeurl=" + SysContants.SZGJ_NOTICEURL);
			log.log(Priority.INFO, url.toString());
			HttpResponseWrapper wrapper = clientManager.execute("GET", url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "充值提交：" + body);
			if("0000".equals(body.trim()) || "0007".equals(body.trim()))
			{
				//进入查询队列
				order.setClbz(SysContants.CZ_RUN);
			}else{
				//充值失败
				order.setClbz(SysContants.CZ_FAILURE);
				order.setCljg("充值失败，上游返回代码：" + body.trim());
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doPay(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer(SysContants.SZGJ_QURL);
		url.append("?clientorderid=" + order.getOrderId());
		
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "查询订单返回：" + body);
			String s[] = body.split("\\|");
			if(s != null && s.length > 0)
			{
				if("2".equals(s[0]))
				{
					//成功
					order.setClbz(SysContants.CZ_SUCCESS);
					order.setCljg("充值成功");
				}else if("4".equals(s[0])){
					//失败
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg("充值失败");
				}else if("7".equals(s[0])){
					//无此订单
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("充值记录已提交，但在上游没有此笔交易记录");
				}else{
					order.setClbz(SysContants.CZ_RUN);
				}
			}else{
				return doQuery(order);
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doQuery(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("18627777692");
		order.setMoney("50");
		order.setOrderId("12312311");
		
		new SzgjServiceImpl().doQuery(order);
	}
}
